//
//  AddPersonController.m
//  Marketing
//
//  Created by Hanen 3G 01 on 16/5/16.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "AddPersonController.h"
#import "MyTextView.h"
#import "CardScanViewController.h"
#import "JudgeNumber.h"

#define STARTX [UIView getWidth:10]

#define InfoCellH 44.0f //每格的高度
#define TitileW 70.0f //每个标题label的宽度
@interface AddPersonController ()<UITextFieldDelegate>
{
    UIView *secondView;
    UITableView *tableView;
}
@property(nonatomic,strong)UITextField    *nameTF;
@property(nonatomic,strong)UITextField    *businessTF;//职位
@property(nonatomic,strong)UITextField    *phoneTF;
@property(nonatomic,strong)UITextField    *emailTF;
@property(nonatomic,strong)MyTextView     *remarkTF;
@end

@implementation AddPersonController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = graySectionColor;
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    
    self.navigationItem.rightBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"HY_nav_icon_scanning" WithAction:@selector(clickToAddClient)];
    
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"添加联系人"];
    [self initUI];
    [self addBtn];
    
}
- (void)goToBack{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)initUI{
    
    tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, KSCreenH -64 -70)];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.backgroundColor = graySectionColor;
    [self.view addSubview:tableView];
    
    NSArray *arr = @[@"姓名",@"职务",@"电话",@"邮箱",@"备注"];
    
    CGFloat tfH    = InfoCellH;
    CGFloat tfW    = KSCreenW - 2 * STARTX - 80.0f;
    
    secondView = [[UIView alloc]initWithFrame:CGRectMake(0, 0 , KSCreenW, 120.0f)];
    secondView.backgroundColor = [UIColor whiteColor];
    [tableView addSubview:secondView];
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCreenW, 40)];
    view.backgroundColor = graySectionColor;
    [secondView addSubview:view];
    
    UILabel *basicLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 , view.height - 20, 200, 20)];
    basicLabel.text = @"联系人信息";
    [ViewTool setLableFont12:basicLabel];
    
    basicLabel.textColor = blackFontColor;
    [view addSubview:basicLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0,view.height - 1, KSCreenW , 1)];
    lineView.backgroundColor = grayLineColor;
    [view addSubview:lineView];
    
    for (int i = 0; i<arr.count; i++) {
        UILabel *label = [ViewTool getLabelWith:CGRectMake(STARTX, view.maxY + InfoCellH*i, 80.0f, InfoCellH) WithTitle:arr[i] WithFontSize:15.0f WithTitleColor:grayFontColor WithTextAlignment:NSTextAlignmentLeft];
        [ViewTool setLableFont13:label];
        [secondView addSubview:label];
        
        if (i != arr.count - 1) {
            UIView *line = [ViewTool getLineViewWith:CGRectMake(STARTX , view.maxY +InfoCellH*(i+1) -1, KSCreenW - STARTX, 1) withBackgroudColor:grayLineColor];
            [secondView addSubview:line];
        }
    }
    
    _nameTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, view.maxY , tfW, tfH) AndStr:@"请填写被拜访人姓名(必填)" WithTrue:YES];
    [secondView addSubview:_nameTF];
    
    
    _businessTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, _nameTF.maxY , tfW, tfH) AndStr:@"请填写被拜访人职务" WithTrue:YES];
    [secondView addSubview:_businessTF];
    
    _phoneTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, _businessTF.maxY , tfW, tfH) AndStr:@"请填写被拜访人手机(必填)" WithTrue:YES];
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    [secondView addSubview:_phoneTF];
    
    _emailTF = [self addTextFieldWithFrame:CGRectMake(STARTX + 80.0f, _phoneTF.maxY , tfW, tfH) AndStr:@"请填写被拜访人邮箱" WithTrue:YES];
    [secondView addSubview:_emailTF];
    
    
    _remarkTF = [[MyTextView alloc] initWithFrame:CGRectMake(STARTX + 80.0f, _emailTF.maxY , KSCreenW - 2 * STARTX - 80.0f, tfH)];
    _remarkTF.placehoder = @" ";
    _remarkTF.textColor = blackFontColor;
    _remarkTF.font = [UIFont systemFontOfSize:16.0f];
    _remarkTF.placehoderColor = placeholderGrayFontColor;
    [secondView addSubview:_remarkTF];
    
    
    secondView.frame = CGRectMake(0, 0, KSCreenW, _remarkTF.maxY);
    UIView *liw = [[UIView alloc]initWithFrame:CGRectMake(0, secondView.height - 1, KSCreenW , 1)];
    liw.backgroundColor = grayLineColor;
    [secondView addSubview:liw];
    
    tableView.contentSize = CGSizeMake(KSCreenW, secondView.maxY);
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    if (_dictionary.count > 0) {
        [self initCardDictionary];
    }
    
}
- (void)addBtn{
    
    UIView *postView = [ViewTool getLineViewWith:CGRectMake(0, KSCreenH - 70 -64, KSCreenW, 70) withBackgroudColor:[UIColor whiteColor]];
    [self.view addSubview:postView];
    
    UIButton * postBtn = [[UIButton alloc] initWithFrame:CGRectMake(13, 8, postView.width - 20, postView.height - 10)];
    [postBtn setTitle:@"确认添加" forState:UIControlStateNormal];
    [postBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [postBtn addTarget:self action:@selector(postDistribute1) forControlEvents:UIControlEventTouchUpInside];
    [postBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_normal.png"] forState:UIControlStateNormal];
    [postBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_press.png"] forState:UIControlStateSelected];
    [postView addSubview:postBtn];
    
    UIView *line3 = [ViewTool getLineViewWith:CGRectMake(0, 0, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [postView addSubview:line3];
    
}
#pragma mark
#pragma mark-------扫一扫添加联系人
- (void)clickToAddClient{
    CardScanViewController *cardScanViewController = [[CardScanViewController alloc] init];
    if([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    }
    cardScanViewController.result = 2;
    [self.navigationController pushViewController:cardScanViewController animated:NO];
}
- (void)initCardDictionary{
    NSArray *array;
    if (_dictionary.count>0) {
        
        
        array = [_dictionary objectForKey:@"name"];
        if (array.count > 0) {
            _nameTF.text = array[0];
        }
        
        
        array = [_dictionary objectForKey:@"title"];
        if (array.count > 0) {
            _businessTF.text = array[0];
        }
        
        
        array = [_dictionary objectForKey:@"mobile"];
        if (array.count > 0) {
            _phoneTF.text = array[0];
        }
        
        array = [_dictionary objectForKey:@"email"];
        if (array.count > 0) {
            _emailTF.text = array[0];
        }
        
    }else{
        
    }

}
#pragma mark
#pragma mark-------确定提交
- (void)postDistribute1
{
    if (_nameTF.text == nil | [_nameTF.text length] == 0 ){
        [self.view makeToast:@"请填写姓名"];
        return;
    }else if (_phoneTF.text == nil | [_phoneTF.text length] == 0 ){
        [self.view makeToast:@"请填写手机号码"];
        return;
    }else if (![JudgeNumber boolenPhone:_phoneTF.text]){
        [self.view makeToast:@"请输入正确的手机号"];
        return;
    }
    
    NSDictionary * dic =@{@"token":TOKEN,@"uid":@(UID),@"cid":@(_cid),@"name":_nameTF.text,@"position":_businessTF.text,@"phone":_phoneTF.text,@"email":_emailTF.text,@"remark":_remarkTF.text};
    NSMutableDictionary *paramDict = [DataTool changeType:dic];
    [DataTool postWithUrl:ADD_NEW_TEL_PERSON parameters:paramDict success:^(id data) {
        id backData = CRMJsonParserWithData(data);
        NSLog(@"添加联系人 返回数据%@",backData);
        if ([backData[@"code"] intValue] == 100) {
//            [self.view makeToast:@"添加成功"];
//            [self.view hideToastActivity];
            [SVProgressHUD showSuccessWithStatus:@"添加成功"];
            [SVProgressHUD dismiss];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
        [self.view makeToast:error.localizedDescription];
    }];
}
-(UITextField *)addTextFieldWithFrame:(CGRect)frame AndStr:(NSString *)placeholder WithTrue:(BOOL)ture
{
    UITextField *textF=[[UITextField alloc]initWithFrame:frame];
    textF.userInteractionEnabled = ture;
    textF.textColor = blackFontColor;
    textF.placeholder=placeholder;
    textF.delegate = self;
    [textF setValue:placeholderGrayFontColor forKeyPath:@"_placeholderLabel.textColor"];
    [ViewTool setTFPlaceholderFont14:textF];
    return textF;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
